/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.loser.csp.service.rpc.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.util.CollectionUtils;

import com.loser.csp.common.gateway.model.BaseRequest;
import com.loser.csp.common.gateway.service.ServiceProcessor;
import com.loser.csp.common.gateway.service.result.BaseBizResultObj;
import com.loser.csp.dal.dao.GoodsMapper;
import com.loser.csp.dal.model.Goods;
import com.loser.csp.service.rpc.enums.BizEnum;
import com.loser.csp.service.rpc.request.GoodsQueryRequest;
import com.loser.csp.service.rpc.result.GoodsQueryResult;

/**
 * 商品查询服务
 * @author shuyan.zsy
 * @version $Id: GoodsQueryService.java, v 0.1 2014-3-1 下午02:09:25 shuyan.zsy Exp $
 */
public class GoodsQueryService implements ServiceProcessor {

    /** logger*/
    private static final Logger logger = Logger.getLogger(GoodsQueryService.class);

    /** 商品查询服务*/
    private GoodsMapper         goodsMapper;

    /** 
     * @return
     * @see com.loser.csp.common.gateway.service.ServiceProcessor#getRequestClass()
     */
    @Override
    public Class<?> getRequestClass() {
        return GoodsQueryRequest.class;
    }

    /** 
     * @param request
     * @param servletRequest
     * @return
     * @see com.loser.csp.common.gateway.service.ServiceProcessor#process(com.loser.csp.common.gateway.model.BaseRequest, com.loser.csp.common.gateway.service.HttpServletRequest)
     */
    @Override
    public BaseBizResultObj process(BaseRequest baseRequest, HttpServletRequest servletRequest) {

        GoodsQueryResult result = new GoodsQueryResult();
        if (logger.isInfoEnabled()) {
            logger.info("接收到商品查询请求，request=" + baseRequest);
        }

        try {
            GoodsQueryRequest request = (GoodsQueryRequest) baseRequest;

            int typeId = request.getProductId();

            if (typeId == 0) {
                logger.warn("商品列表查询失败，产品ID为空");
                result.setSuccess(false);
                result.setErrorCode(BizEnum.ILLEAGE_PARAM.name());
                result.setErrorDesc(BizEnum.ILLEAGE_PARAM.getDesc());
                return result;
            }

            List<Goods> goodsList = goodsMapper.selectListByTypeId(typeId);
            if (CollectionUtils.isEmpty(goodsList)) {
                logger.warn("查询商品列表为空,request={0}" + request);
            }
            result.setGoodsList(goodsList);
            return result;

        } catch (Exception e) {

            logger.error("商品查询出错", e);
            result.setSuccess(false);
            result.setErrorCode(BizEnum.SYSTEM_EXCEPTION.name());
            result.setErrorDesc(BizEnum.SYSTEM_EXCEPTION.getDesc());
            return result;
        }
    }

}
